#include <stdio.h>
#include <math.h>
#include <mpi.h>
#define M 128
#define N 128
#define NITER 256

typedef struct tagComplex {
  float real;
  float img;
} Complex;

int cal_pixel(Complex d, float b, int n);

Complex add_complex( Complex z1, Complex z2 ) {
  Complex z3;
  z3.real = z1.real + z2.real;
  z3.img  = z1.img  + z2.img;
  return z3;
}

Complex mult_complex( Complex z1, Complex z2 ) {
  Complex z3;
  z3.real = z1.real*z2.real - z1.img*z2.img;
  z3.img  = z1.real*z2.img + z1.img*z2.real;
  return z3;
}

float mod_complex( Complex z ) {
  return sqrt(z.real*z.real+z.img*z.img);
}


int cal_pixel(Complex d, float b, int n){
  int count = 1;
  Complex z; z.real = 0; z.img = 0;

  while ( (mod_complex(z) < b) && (count<n) ){
    z = add_complex(mult_complex(z,z),d);
    count++;



main(int argc, char **argv){
  unsigned char color[M*N];
  FILE *fp;
/* your computations */
  float b = 2;
  float dx = 2*b/(N-1);
  float dy = 2*b/(M-1);
  float dreal, dimg;
  Complex d;
  int i, j;




  fp = fopen("color.txt","w");
  for (j = 0; j < N; j++) {
    d.real = j*dx - b;
    for (i = 0; i < M; i++){
      d.img = i*dy - b;
      color[i+j*M] = cal_pixel(d, b, NITER);
      //          color[i+j*M] = 0;

      //      printf( "%hhu", color[i+j*M]);
      fprintf(fp, "%hhu", color[i+j*M]);
    }
    fprintf(fp, "\n");
  }
  fclose(fp);
}



  }
  return count;
}